//
//  FamilyHotlineViewController.m
//  Cunli
//
//  Created by 湛亚 on 15/8/4.
//  Copyright (c) 2015年 lilubin. All rights reserved.
//

#import "FamilyHotlineViewController.h"
#import "MyDataService.h"
#import "NSString+MD5.h"
#import "Mediator.h"
#import "UIImageView+WebCache.h"
#import "ChatViewController.h"
#import "UserInfoDetail.h"
#import "FamilyCallViewController.h"

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)

@interface FamilyHotlineViewController (){
    
    UIImageView *headPicImg;
    UILabel *nameLabel;
    UIButton *callButton,*chatButton,*videoButton;
    NSString *phoneString;
    UserInfoDetail *userInfo;
}

@end

@implementation FamilyHotlineViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.title=@"亲情热线";
    self.view.backgroundColor=[UIColor whiteColor];
    self.isInvisible=YES;
    [self createView];
    [self creareNavButton];
    [self getFamilyInfo];
}

-(void)creareNavButton{
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(0, 0, 28, 28);
    [leftBtn setImage:[UIImage imageNamed:@"regback01"] forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    leftBtn.contentEdgeInsets = UIEdgeInsetsMake(0, -20, 0, 0);
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];
    self.navigationItem.leftBarButtonItem = leftBarItem;
}

-(void)goBack{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"orSendRequest" object:@YES];
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)createView{
    
    //头像
    headPicImg=[[UIImageView alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-80)/2.0, SCREEN_HEIGHT/2.0-220, 80, 80)];
    headPicImg.image=[UIImage imageNamed:@"headplacehold.png"];
    [self.view addSubview:headPicImg];
    
    //姓名
    nameLabel=[[UILabel alloc]initWithFrame:CGRectMake((SCREEN_WIDTH-100)/2.0, SCREEN_HEIGHT/2.0-130, 100, 30)];
    nameLabel.textAlignment=1;
    [self.view addSubview:nameLabel];
    
    //电话，对话，视频按钮
    float btnWeight=(SCREEN_WIDTH-80)/3.0;
    callButton=[UIButton buttonWithType:UIButtonTypeCustom];
    callButton.frame=CGRectMake(20, SCREEN_HEIGHT/2.0-30, btnWeight, 30);
    [callButton setImage:[UIImage imageNamed:@"yuyin.png"] forState:UIControlStateNormal];
    [callButton addTarget:self action:@selector(callMediator) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:callButton];
    
    chatButton=[UIButton buttonWithType:UIButtonTypeCustom];
    chatButton.frame=CGRectMake(40+btnWeight, SCREEN_HEIGHT/2.0-30, btnWeight, 30);
    [chatButton setImage:[UIImage imageNamed:@"chat.png"] forState:UIControlStateNormal];
    [chatButton addTarget:self action:@selector(chatWithMediator) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:chatButton];
    
    videoButton=[UIButton buttonWithType:UIButtonTypeCustom];
    videoButton.frame=CGRectMake(60+btnWeight*2, SCREEN_HEIGHT/2.0-30, btnWeight, 30);
    [videoButton setImage:[UIImage imageNamed:@"video.png"] forState:UIControlStateNormal];
    [videoButton addTarget:self action:@selector(videoWithMediator) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:videoButton];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleCallNotification:) name:@"callOutWithChatter" object:nil];
}

- (void)handleCallNotification:(NSNotification *)notification
{
    id object = notification.object;
    if ([object isKindOfClass:[NSDictionary class]]) {
        //开始call
        self.isInvisible = YES;
    }
    else
    {
        //结束call
        self.isInvisible = NO;
    }
}

-(void)getFamilyInfo{
    
    NSMutableDictionary *CheckParams =[[NSMutableDictionary alloc]init];
    [CheckParams setObject:[Config getOwnUserID] forKey:@"userid"];
    [CheckParams setObject:self.touserid forKey:@"touserid"];
    NSString *resultSign = [NSString getSignFromParam:CheckParams andSession:[Config getOwnSession]];
    [CheckParams setObject:resultSign forKey:@"sign"];
    [MyDataService requestURL:@"getinfo_addressbook.php" httpMethod:@"GET" params:CheckParams fileDatas:nil complection:^(id result) {
        
        NSLog(@"%@",result);
        
        if ([[result objectForKey:@"response"] isEqualToString:@"getinfo_member"]) {
            NSDictionary *userInfoDic=[result objectForKey:@"userinfo"];
            //userInfo=[[UserInfoDetail alloc]initWithDictionary:userInfoDic];
            userInfo=[UserInfoDetail yy_modelWithDictionary:userInfoDic];
            [headPicImg setImageWithURL:[NSURL URLWithString:userInfo.pic] placeholderImage:[UIImage imageNamed:@"default_touxiang.png"]];
            nameLabel.text=userInfo.truename;
            
        }else{
            
            MBProgressHUD *hud = [Config createHUD];
            hud.mode = MBProgressHUDModeCustomView;
            hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
            hud.labelText = @"获取详情失败!";
            [hud hide:YES afterDelay:1];
            chatButton.userInteractionEnabled=NO;
            callButton.userInteractionEnabled=NO;
            videoButton.userInteractionEnabled=NO;
        }
        
    }failureAction:^{
        
    }];
    
}

- (void)callMediator {
    
    //    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"tel:%@",userInfo.mobile];
    //    UIWebView * callWebview = [[UIWebView alloc] init];
    //    [callWebview loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:str]]];
    //    [self.view addSubview:callWebview];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"callOutWithChatter" object:@{@"chatter":userInfo.huanxin_username, @"type":[NSNumber numberWithInt:eCallSessionTypeAudio]}];
}

- (void)chatWithMediator {
    
    ChatViewController *chatVC=[[ChatViewController alloc]initWithChatter:userInfo.huanxin_username isGroup:NO];
    chatVC.title=userInfo.truename;
    chatVC.chatPicUrl=userInfo.pic;
    [self.navigationController pushViewController:chatVC animated:YES];
}

- (void)videoWithMediator {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"callOutWithChatter" object:@{@"chatter":userInfo.huanxin_username, @"type":[NSNumber numberWithInt:eCallSessionTypeVideo]}];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
